package file;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.File;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class FileServer extends JFrame implements Runnable{

	
	ServerSocket ss;
	Socket s;
	FileServerThread fst;
	String path;
	
	private JPanel contentPane;
	private JScrollPane scrollPane;
	 JTextArea textArea;
	private JButton btnNewButton;
	private JTextField textField;
	private JButton btnNewButton_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FileServer frame = new FileServer();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FileServer() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 285);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getScrollPane());
		contentPane.add(getBtnNewButton());
		contentPane.add(getTextField());
		contentPane.add(getBtnNewButton_1());
	}
	public void run(){
		try{
			textArea.append("서버 시작\n");
			ss = new ServerSocket(5555);
			while(true){
			s = ss.accept();
			textArea.append("어떤 시끼가 들어왔어요\n");
			fst = new FileServerThread(s,this);
			
			}
			
		}catch(Exception e){
			
		}
		
		
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 10, 410, 190);
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("선택");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					JFileChooser jc = new JFileChooser();
					int yn = jc.showOpenDialog(FileServer.this);
					if(yn==JFileChooser.APPROVE_OPTION){
						path = jc.getSelectedFile().getPath();
					
						textField.setText(path);
						
						
					}
				
				}
			});
			btnNewButton.setBounds(216, 209, 97, 23);
		}
		return btnNewButton;
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setBounds(12, 210, 192, 21);
			textField.setColumns(10);
		}
		return textField;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("전송");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					new Thread(FileServer.this).start();
				
				
				
				}
			});
			btnNewButton_1.setBounds(325, 209, 97, 23);
		}
		return btnNewButton_1;
	}
}
